<template>
  <div class="register-page">
    <!-- 导航栏 -->
    <van-nav-bar title="面经注册" />

    <!-- 表单 -->
    <van-form @submit="onSubmit">
      <van-field
        v-model="username"
        name="username"
        label="用户名"
        placeholder="用户名"
        :rules="[
          { required: true, message: '请填写用户名' },
          { pattern: /^\w{5,}$/, message: '用户名至少5位字符' },
        ]"
      />
      <!--
          placeholder="函数校验"
          :rules="[{ validator, message: '请输入正确内容' }]"
         -->
      <van-field
        v-model="password"
        type="password"
        name="password"
        label="密码"
        placeholder="密码"
        :rules="[
          { required: true, message: '请填写密码' },
          { pattern: /^\w{6,}$/, message: '密码至少6位字符' },
        ]"
      />
      <div style="margin: 16px">
        <van-button block type="info" native-type="submit">注册账号</van-button>
      </div>
    </van-form>
    <!-- 跳转到注册页 -->
    <router-link class="link" to="/login">有账号?去登录</router-link>
  </div>
</template>

<script>
// 引入发送注册请求
import { register } from '@/api/user'
export default {
  name: 'register-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      console.log('submit', values)
      // 往后台发送注册请求 - 弹框,跳转至登录页
      await register(values)
      this.$toast.success('恭喜注册成功')
      this.$router.push('/login')
      // 若有报错, 在request中统一设置响应拦截器
    }
  }
}
</script>

<style lang="less" scoped>
.link {
  color: #069;
  font-size: 12px;
  padding-right: 20px;
  float: right;
}
</style>
